package com.wangyu.generator.core.impl;

import com.google.common.collect.Maps;
import com.wangyu.generator.core.Generator;
import com.wangyu.generator.core.context.CodeContext;
import com.wangyu.generator.core.context.GeneratorType;
import java.util.Iterator;
import java.util.Map;
import org.apache.commons.collections.MapUtils;

/**
 * 分发生成代码适配器
 *
 * @author: zhangQ
 * @date: 2016-8-18 17:03
 */
public class GeneratorFacade implements Generator {

  private Map<GeneratorType, Generator> generatorMap = Maps.newHashMap();

  public void defaultGenerator(CodeContext context, GeneratorType configType) {
    getGenerator(configType).defaultGenerator(context, configType);
  }

  public void pluginGenerator(CodeContext context, GeneratorType configType) {
    getGenerator(configType).pluginGenerator(context, configType);
  }

  public void setGeneratorMap(Map<String, Generator> map) {
    if (MapUtils.isNotEmpty(map)) {
      Iterator<Map.Entry<String, Generator>> iterator = map.entrySet().iterator();
      while (iterator.hasNext()) {
        Map.Entry<String, Generator> entry = iterator.next();
        String key = entry.getKey();
        GeneratorType configType = GeneratorType.getByType(key);
        if (null == configType) {
          throw new RuntimeException("根据key找不到生成代码的Generator，key = " + key);
        }
        this.generatorMap.put(configType, entry.getValue());
      }
    }
  }

  public Generator getGenerator(GeneratorType configType) {
    return generatorMap.get(configType);
  }
}
